<?
$r = "../";
require_once($r."_engine/header_nocache.php");
require_once($r."_config/db.php");
require_once($r."_config/site.php");
require_once($r."_engine/c_sql_table.php");
require_once($r."_engine/functions.php");

require_once($r."_inc/c_table_generator2.php");

$title = "Download Statistics";
include "_header.php";

?>
<p class="text">
<?

function stats_by_condition($cond)
{
	if ($cond != "") $cond = " WHERE ".$cond;
	
	$res = @mysql_fetch_array(execute_query(
		"SELECT ".
			"COUNT(*) as total_cnt, ".
			"SUM(total_time) as total_time, ".
			"SUM(speed_of_download) as avr_speed, ".
			"SUM(size_of_download) as total_size ".
		"FROM download_stats" . $cond . ";"
	));
	
	if ($res["total_cnt"] == 0)
	{
		$res["total_cnt"] = "";
	}
	else
	{
		$res["avr_speed"] = $res["avr_speed"] / $res["total_cnt"];
	}

	return $res;
}


function stats_all()
{
	return stats_by_condition("");
}

function stats_day($dt)
{
	// Make condition
	$dt1 = $dt;
	$dt2 = strtotime("+1 day", $dt1);
	$cond =
		"download_dt >= '".date("Y-m-d", $dt1)." 00:00:00' AND ".
		"download_dt <  '".date("Y-m-d", $dt2)." 00:00:00'";

	return stats_by_condition($cond);
}

function stats_week($dt)
{
	// Get day of the week: "0" (Sunday) to "6" (Saturday)
	$day_of_the_week = date("w", $dt);

	// Change day of the week to: "0" (Monday) to "6" (Sunday)
	if ($day_of_the_week > 0) $day_of_the_week--; else $day_of_the_week=6;

	// Make condition
	$dt1 = strtotime("-$day_of_the_week day", $dt);
	$dt2 = strtotime("+7 day", $dt1);
	$cond =
		"download_dt >= '".date("Y-m-d", $dt1)." 00:00:00' AND ".
		"download_dt <  '".date("Y-m-d", $dt2)." 00:00:00'";

	return stats_by_condition($cond);
}

function size_to_str($x)
{
	if ($x == "") return "";
	
	$a = array("", "K", "M", "G", "T");
	
	for ($i=0;  round($x/1000) >= 1;  $x/=1000, $i++)
	{
		if ($i+1 >= count($a)) break;
	}
	
	return round($x) . " " . $a[$i] . "b";
}

function speed_to_str($x)
{
	if ($x == "") return "";
	
	$a = array("", "K", "M", "G", "T");
	
	for ($i=0;  round($x/1000) >= 1;  $x/=1000, $i++)
	{
		if ($i+1 >= count($a)) break;
	}
	
	return round($x) . " " . $a[$i] . "bps";
}

$view_last_days = 30;
if (isset($all_period))
{
	$row = @mysql_fetch_array(@mysql_query(
		"SELECT MIN(date_time) as date_time_min FROM download_stats WHERE YEAR(date_time) >= 2004;"
	));
	if (isset($row["date_time_min"]))
	{
		$t1 = strtotime("now");
		$t2 = strtotime($row["date_time_min"]);
		$view_last_days = floor(abs($t1-$t2)/3600/24);
	}
}


$tg = new CTableGenerator2(1, true);
$tg->td_class["default"] = 'class="textStd"';
$tg->td_style["red"] = 'style="color:#CC0000; font-weight:bold;"';


$tg->PrintHeader();
$tg->Cell("", "header|begin");
$tg->Cell("Date", "header");
$tg->Cell("Total cnt", "header");
$tg->Cell("Time", "header");
$tg->Cell("Avr Speed", "header");
$tg->Cell("Traffic", "header|end");

function print_line($stats, $t1, $t2, $is_h, $is_we)
{
	global $tg;
	$opt_h = ($is_h ? "header|" : "");
	$opt_we = ($is_we ? "|td_style=red" : "");
	
	$tg->Cell($t1,						$opt_h."begin".$opt_we);
	$tg->Cell($t2,						$opt_h);
	$tg->Cell($stats["total_cnt"],		$opt_h);
    $tg->Cell($stats["total_time"],		$opt_h);
	$tg->Cell(speed_to_str($stats["avr_speed"]),		$opt_h);
	$tg->Cell(size_to_str($stats["total_size"]),		$opt_h."end");
}

for ($i=0; $i<$view_last_days; $i++)
{
	$dt = strtotime("-$i day");
	$opt_h = "";
	$opt_we = "";
	
	if ($i==0)
	{
		// Print total row
		print_line(stats_all(), "", "Total", true, false);
	}
	if ($i==0 || date("w",$dt)==0)
	{
		// Print weekly row
		print_line(stats_week($dt), "", "Weekly Total", true, false);
	}

	print_line(stats_day($dt),
		date("D", $dt),
		date("j M Y", $dt), 
		false,
		(date("w",$dt)==0 || date("w",$dt)==6) // Red color for highlight weekends
	);
}
$tg->PrintFooter();

echo "<br>";

if (!isset($all_period))
	echo "<a href=\"download_stats.php?all_period=1&".generate_uniq_url_param()."\">View all period</a>\n";

include "_footer.php";
?>
